<?php
/* 
*    This program is free software: you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
*    Version 1.0, 22th June 2011
*    Version 0.1, 14th June 2008
*/
include ("HttpClient.class.php");

class Mirrorbow extends HttpClient {
    var $mirrorbow_device;
    var $slogin = true;
    var $mb_debug_output = false;
    var $mb_source = '/ports.xml';
    var $LCDControlParam = array(
            'mode_normal'  => '1',
            'mode_scroll'  => '2',
            'cursor_off'   => '3',
            'cursor_on'    => '4',
            'clrscr'       => '5',
            'monitor_off'  => '6',
            'monitor_POP3' => '7',
            'monitor_serv' => '8',
            'monitor_env'  => '9',
            'set_C'	   => 'A',
            'set_F'	   => 'B',
    );
    var $portNumbers = array(
            '1'  => 'port1',
            '2'  => 'port2',
            '3'  => 'port3',
            '4'  => 'port4',
            '5'  => 'port5',
            '6'  => 'port6',
    );

    function Mirrorbow() {
        $mirrorbow_device=array();
    }

    function HttpRequestHeader($dev_name) {
        $this->HttpClient($this->mirrorbow_device[$dev_name]);
        $this->setUserAgent('Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.3a) Gecko/20021207');
    }

    function getDeviceDetails($dev_name) {
        $this->HttpRequestHeader($dev_name);
        if (!$this->get($this->getSource())) {
            #die('An error occurred: '.$this->getError());
            #die('0.00');
            $ret_val = "0";
        }
        $ret_val = $this->xml2array($this->getContent());

        foreach ($ret_val[0]['elements'] as $key => $value) {
            $count = 0;
            foreach ($value as $key2) {
                $temp_aprr[$count]=$key2;
                if ($count==1) {
                    $state[$temp_aprr[0]] = $temp_aprr[1];
                    $count=0;
                } else {
                    $count++;
                }
            }
        }
        return $state;
    }

    function LCDControl($dev_name, $operation) {
        $this->HttpRequestHeader($dev_name);
        $this->get('/index.htm', array('LCD' => $this->LCDControlParam[$operation]));

        $this->debug();
    }

    function LCDText($dev_name, $textparam) {
        $this->HttpRequestHeader($dev_name);
        $this->post('/lcd.htm', array('txt' => $textparam));

        $this->debug();
    }

    function LCDSetPosition($dev_name, $line, $col) {
        $this->HttpRequestHeader($dev_name);
        if($col<=9) {
            $col= "0" . $col;
        }
        $realpar=$line.$col;
        $this->get('/index.htm', array('POS' => $realpar));

        $this->debug();
    }

    function setPortValue($dev_name, $portnr, $value) {
        $this->HttpRequestHeader($dev_name);
        $realpar="P" . $portnr;
        if($value<=9) {
            $value= "00" . $value;
        }
        elseif($value<=99 && $value>=10) {
            $value= "0" . $value;
        }
        $this->get('/index.htm', array($realpar => $value));

        $this->debug();
    }

    function setPortValueFromBin($dev_name, $portnr, $array_value) {
        $dec_value = $this->binArray2dec($array_value);
        $this->setPortValue($dev_name, $portnr, $dec_value);
    }

    function getPortValueToBin($dev_name, $portnr) {
        $device_details = $this->getDeviceDetails($dev_name);
        $value = $device_details[$this->portNumbers[$portnr]];
        return $this->dec2binArray($value);
    }

    function setPortDirection($dev_name, $portnr, $value) {
        $this->HttpRequestHeader($dev_name);
        $realpar="D" . $portnr;
        if($value<=9) {
            $value= "00" . $value;
        }
        elseif($value<=99 && $value>=10) {
            $value= "0" . $value;
        }
        $this->get('/index.htm', array($realpar => $value));

        $this->debug();
    }

    function setADC($dev_name, $value) {
        $this->HttpRequestHeader($dev_name);
        if($value<=9) {
            $value= "0" . $value;
        }
        $this->get('/index.htm', array('ADC' => $value));

        $this->debug();
    }

    function calculateDEWPoint($temp, $hum) {
        return 	round(((sqrt(sqrt(sqrt(($hum/100)))))*(112+(0.9*$temp))+(0.1*$temp)-112),1);
    }

    function debug() {
        if ($this->mb_debug_output) {
            echo $this->getContent();
        }
    }

    function setDebug($debug) {
        $this->mb_debug_output = $debug;
    }

    function setSource($source) {
        $this->mb_source = $source;
    }

    function getSource() {
        return $this->mb_source;
    }

    function dec2binArray($eightBitDec) {
        $result = array(0,0,0,0,0,0,0,0);
        $bin = decbin($eightBitDec);
        $bin = substr("00000000",0,8 - strlen($bin)) . $bin;
        for($i=0; $i < strlen($bin); $i++) {
            $result[$i] = $bin[$i];
        }
        return array_reverse($result);
    }

    function binArray2dec($eightBitBin) {
        $bin = "";
        $true_binary_value = array_reverse($eightBitBin);
        foreach($true_binary_value as $key => $val) {
            $bin .= $val;
        }
        return bindec($bin);
    }

    function getBitValue($eightBitBin, $bitNo) {
        if ($eightBitBin[$bitNo]) {
            return true;
        } else {
            return false;
        }
    }
    
    function setBitValue($device, $eightBitBin, $bitNo, $val) {
        $eightBitBin[$bitNo] = $val;
        $this->setPortValueFromBin($device,1,$eightBitBin);
    }

    function xml2array($xml) {
        $xmlary = array();

        $reels = '/<(\w+)\s*([^\/>]*)\s*(?:\/>|>(.*)<\/\s*\\1\s*>)/s';
        $reattrs = '/(\w+)=(?:"|\')([^"\']*)(:?"|\')/';

        preg_match_all($reels, $xml, $elements);

        foreach ($elements[1] as $ie => $xx) {
            $xmlary[$ie]["name"] = $elements[1][$ie];

            if ($attributes == trim($elements[2][$ie])) {
                preg_match_all($reattrs, $attributes, $att);
                foreach ($att[1] as $ia => $xx)
                    $xmlary[$ie]["attributes"][$att[1][$ia]] = $att[2][$ia];
            }

            $cdend = strpos($elements[3][$ie], "<");
            if ($cdend > 0) {
                $xmlary[$ie]["text"] = substr($elements[3][$ie], 0, $cdend - 1);
            }

            if (preg_match($reels, $elements[3][$ie]))
                $xmlary[$ie]["elements"] = $this->xml2array($elements[3][$ie]);
            else if ($elements[3][$ie]) {
                $xmlary[$ie]["text"] = $elements[3][$ie];
            }
        }

        return $xmlary;
    }

}
?>
